home *** CD-ROM | disk | FTP | other *** search
/ United Public Domain Gold 2 / United Public Domain Gold 2.iso / utilities / pu745.dms / pu745.adf / GLOBE099.LHA / Ami-Globe / read_bin.c < prev    next >
C/C++ Source or Header  |  1994-11-17  |  17KB  |  578 lines

  1. /************************************************************************/
  2. /*                                                                      */
  3. /*      fichier         : read_bin.c                                    */
  4. /*      projet          : amiglobe                                      */
  5. /*      date création   : 27/08/94                                      */
  6. /*      commentaire     : lecture des fichiers new_lac.Pt_bin           */
  7. /*                        création du tableau des points                */
  8. /*      révision        : $VER: read_bin.c 1.002 (27 Aug 1994) */
  9. /*      copyright       : Olivier Collard, Thomas Landspurg             */
  10. /*                                                                      */
  11. /************************************************************************/
  12.  
  13.  
  14. /************************************************************************/
  15. /*      includes                                                        */
  16. /************************************************************************/
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <exec/types.h>
  21. #include <stat.h>
  22. #include <libraries/dos.h>
  23. #include <clib/dos_protos.h>
  24.  
  25. #include "amiglobe_types.h"
  26. #include "read_bin.h"
  27. #include "database_types.h"
  28. #include "map_function_protos.h"
  29. #include "map_function_types.h"
  30. #include "database_protos.h"
  31.  
  32. /************************************************************************/
  33. /*      defines                                                         */
  34. /************************************************************************/
  35.  
  36. #define NB_MAX_PT_LIGNE 7000    /* Defini le nombre maximum de pt dans un segment */
  37.                                 /* , utilise lors de read map seulement           */
  38.  
  39. /************************************************************************/
  40. /*      variables externes                                              */
  41. /************************************************************************/
  42. extern CLIP clip_max;
  43. extern BORDER  *tab_vect;
  44. extern BPTR    bptr_big;
  45. extern int     max_border;
  46. extern int     max_country;          /* Nombre max de pays       */
  47. extern COUNTRY *tab_country;
  48. short int * tableau=NULL;
  49. int taille_tableau=0;
  50. /*********************************************************** read_map() ***/
  51.  
  52. BOOL 
  53. read_map(char *p_filename)
  54. {
  55.     char    Buffer_Name[100];
  56.     FILE    *input;
  57.     int     x, y,code;
  58.     char    c1,c2,c;
  59.     int     mx,my,MX,MY;
  60.     int     i;
  61.  
  62.     int     flg_border,flg_zone;
  63.     int     flg_end;
  64.     int     num_bord;
  65.     int     Last_Code=0;
  66.     /* Vecteur courant */
  67.     struct  vect *tab_v;
  68.     struct  vect *v;
  69.     int     nb_vect;
  70.     BOOL    Flg_Err=TRUE;
  71.  
  72.  
  73.     flg_end=FALSE;
  74.  
  75.     sprintf(Buffer_Name,"%s.pt",p_filename);
  76.  
  77.     if (input = fopen(Buffer_Name,"r"))
  78.     {
  79.         /* On alloue la mémoire pour le buffer de lecture ligne */
  80.         tab_v=(VECT *)malloc((NB_MAX_PT_LIGNE)*sizeof(struct vect));
  81.  
  82.         /* Premiere chose, la taille max de notre carte...*/
  83.  
  84.         fscanf(input,"%ld%ld%ld%ld",
  85.                 &clip_max.minx,&clip_max.miny,
  86.                 &clip_max.maxx,&clip_max.maxy);
  87. /*              Pref.clip_cur=clip_max;*/
  88.  
  89.  
  90.  
  91.         /********************************************************/
  92.         /* Puis la liste des bords de pays. C'est une liste     */
  93.         /* de points terminee par le couple -1 -1               */
  94.         /********************************************************/
  95.  
  96.  
  97.         num_bord=0;
  98.         flg_zone=TRUE;
  99.  
  100.         do
  101.         {
  102. #ifdef  TOMDEBUG
  103.             printf("Vecteur numero:%d\n",num_bord);
  104. #endif
  105.             v=tab_v;
  106.  
  107.             /* Lecture de composantes d'un bord */
  108.             mx=100000;
  109.             my=100000;
  110.             MX=-100000;
  111.             MY=-100000;
  112.  
  113.             flg_border=TRUE;
  114.             nb_vect=0;
  115.             do
  116.             {
  117.                 c1=getc(input);
  118.                 c2=getc(input);
  119.                 if(c2=='#')
  120.                 {
  121.                     do
  122.                     {
  123.                             c=getc(input);
  124.                     }  while((c!=0x0a)&&(c!=EOF));
  125.                     flg_border=FALSE;
  126.  
  127.                 }
  128.                 else
  129.                 {
  130.                     ungetc(c2,input);
  131.                     ungetc(c1,input);
  132.             
  133.                     fscanf(input,"%ld %ld %ld",&code,&x,&y);
  134.                     v->x=x;
  135.                     v->y=-y;
  136.                     v->Code=code;
  137.                     tstpt(v->x,v->y,&mx,&my,&MX,&MY);
  138. /*                  printf("x:%d y:%d  -> :%ld %ld\n",x,y,v->x,v->y);
  139. */
  140.                     v++;
  141.                     nb_vect++;
  142.                 }
  143.                 if(nb_vect>=NB_MAX_PT_LIGNE)
  144.                 {
  145.                     printf("Error :max number of pt exceeded -> %d \n",num_bord);
  146.                     flg_border=FALSE;
  147.                 }
  148.             }  while(flg_border==TRUE);
  149.  
  150.             /* On s'assure que le dernier point est bien de niveau 5 */
  151.             if (nb_vect>1)
  152.                 v[-1].Code=5;
  153.  
  154.             /* On s'assure que le code ud premier pt est bien */
  155.             /* un indicateur de nveau segment                 */
  156.  
  157.             if(tab_v[0].Code<1000)
  158.             {
  159.                 tab_v[0].Code=Last_Code;
  160.             }
  161.             Last_Code=tab_v[0].Code;
  162.  
  163.             tab_vect[num_bord].p_vect=(VECT *)malloc((nb_vect+10)*
  164.                                             sizeof(struct vect));
  165.             for(i=0;i<nb_vect;i++)
  166.             {
  167.                 tab_vect[num_bord].p_vect[i]=tab_v[i];
  168.             }
  169.             tab_vect[num_bord].b_clip.minx=mx;
  170.             tab_vect[num_bord].b_clip.miny=my;
  171.             tab_vect[num_bord].b_clip.maxx=MX;
  172.             tab_vect[num_bord].b_clip.maxy=MY;
  173.  
  174. /*                      printf(" Il y a eu:%d pt dans le bord %d\n",nb_vect,num_bord);
  175.             printf("Code:%d Numero:%d\n",tab_v[0].Code,num_bord);
  176. */
  177.  
  178.             tab_vect[num_bord].nb_vect=nb_vect;
  179.             if(num_bord>MAX_BORDER)
  180.             {
  181.                     printf("ERREUR, max number of border exceeded!\n");
  182.             }
  183.             else
  184.             {
  185.                     num_bord++;
  186.             }
  187.             if(tst_end(input)==TRUE)
  188.             {
  189.                     flg_zone=FALSE;
  190.             }
  191.         }  while(flg_zone==TRUE);
  192.         Flg_Err=FALSE;  
  193.         max_border=num_bord;
  194.         free(tab_v);
  195.     }
  196.     return(Flg_Err);
  197. }
  198.  
  199.  
  200. /*********************************************************** read_map() ***/
  201.  
  202. BOOL 
  203. read_map_bin(char *p_filename)
  204. {
  205.     char    Buffer_Name[100];
  206.     char    Buffer_Name_Big[100];
  207.     FILE    *input;
  208.     int     mx,my,MX,MY;
  209.     int     i;
  210.  
  211.     int     flg_zone;
  212.     int     flg_end;
  213.     int     num_bord;
  214.     int     Last_Code=0;
  215.     /* Vecteur courant */
  216.     short   int     *tab_temp;
  217.     char    *p_deb;
  218.     struct  vect *v;
  219.     short   int     nb_vect;
  220.     int     nb_read,taille;
  221.     struct  stat    my_stat;
  222.     short   int     minx_real,miny_real,maxx_real,maxy_real;
  223.     BOOL    Flg_Err=TRUE;
  224.  
  225.  
  226.     flg_end=FALSE;
  227.  
  228.     sprintf(Buffer_Name,"%s.pt_bin_small",p_filename);
  229.     sprintf(Buffer_Name_Big,"%s.pt_bin",p_filename);
  230.  
  231.     if ((input = fopen(Buffer_Name,"r"))
  232.             &&(bptr_big=Open(Buffer_Name_Big,MODE_OLDFILE)))
  233.     {
  234.         /* Premiere chose, la taille max de notre carte...*/
  235.  
  236.         fread(&minx_real,sizeof(short int),1,input);
  237.         fread(&miny_real,sizeof(short int),1,input);
  238.         fread(&maxx_real,sizeof(short int),1,input);
  239.         fread(&maxy_real,sizeof(short int),1,input);
  240.         clip_max.minx=minx_real;
  241.         clip_max.miny=miny_real;
  242.         clip_max.maxx=maxx_real;
  243.         clip_max.maxy=maxy_real;
  244. /*              clip_cur=clip_max;*/
  245.  
  246.  
  247.  
  248.         stat(Buffer_Name,&my_stat);
  249.         
  250.         taille=my_stat.st_size;
  251.         taille=taille-4*sizeof(short int);
  252.  
  253.  
  254.  
  255.         tab_temp=(short int *)malloc(taille);
  256.         tableau=tab_temp;
  257.         taille_tableau=taille;
  258.         if (tab_temp==NULL)
  259.         {
  260.             printf("ERROR: Not Enough Memory!\n");
  261.         }
  262.         else
  263.         {
  264.             nb_read=fread(tab_temp,1,taille,input);
  265.             /* lecture du reste du fichier */
  266.             if (nb_read!=taille)
  267.             {
  268.                 printf("ERROR: File truncated\n");
  269.             }
  270.             p_deb=(char *)tab_temp;
  271.  
  272.  
  273.             num_bord=0;
  274.             flg_zone=TRUE;
  275.  
  276.             do
  277.             {
  278. #ifdef  TOMDEBUG
  279.                 printf("Vecteur numero:%d\n",num_bord);
  280. #endif
  281.                 /* Lecture de composantes d'un bord */
  282.                 mx=100000;
  283.                 my=100000;
  284.                 MX=-100000;
  285.                 MY=-100000;
  286.  
  287.                 /* Recupere la position ds le grand fichier */
  288.                 tab_vect[num_bord].offset=*((LONG *)tab_temp);
  289.                 tab_vect[num_bord].p_vect_maxres=NULL;
  290.                 tab_vect[num_bord].nb_vect_bis=-1;
  291.                 tab_temp+=sizeof(LONG)/sizeof(short int);
  292.  
  293.                 nb_vect=*tab_temp++;/* 5eme mot = nb de vecteurs */
  294.                 tab_vect[num_bord].p_vect=(struct vect *)tab_temp;
  295.                 tab_temp+=nb_vect*3;
  296.                 v=tab_vect[num_bord].p_vect;
  297.  
  298.                 /* On cree la zone de clipping, mais en */
  299.                 /* excluant les lacs et rivieres        */
  300.                 for (i=0;i<nb_vect;i++)
  301.                 {
  302.                     tstpt(v->x,v->y,&mx,&my,&MX,&MY);
  303.                     v++;
  304.                 }
  305.  
  306.                 v=tab_vect[num_bord].p_vect;                
  307.                 /* On s'assure que le dernier point est bien de niveau 5 */
  308.                 if (nb_vect>1)
  309.                     v[nb_vect-1].Code=5;
  310.  
  311.                 /* On s'assure que le code du premier pt est bien */
  312.                 /* un indicateur de nouveau segment                       */
  313.  
  314.                 if (tab_vect[num_bord].p_vect[0].Code<1000)
  315.                 {
  316.                     tab_vect[num_bord].p_vect[0].Code=Last_Code;
  317.                 }
  318.                 Last_Code=tab_vect[num_bord].p_vect[0].Code;
  319.  
  320.                 tab_vect[num_bord].nb_vect=nb_vect;
  321.  
  322.                 tab_vect[num_bord].b_clip.minx=mx;
  323.                 tab_vect[num_bord].b_clip.miny=my;
  324.                 tab_vect[num_bord].b_clip.maxx=MX;
  325.                 tab_vect[num_bord].b_clip.maxy=MY;
  326.  
  327. /*
  328.                 printf(" Il y a eu:%d pt dans le bord %d\n",nb_vect,num_bord);
  329. */
  330.  
  331.                 if (num_bord>MAX_BORDER)
  332.                 {
  333.                     printf("ERROR, max number of border exceeded!\n");
  334.                 }
  335.                 else
  336.                 {
  337.                     num_bord++;
  338.                 }
  339.                 if ((char *)tab_temp>=taille+p_deb)
  340.                 {
  341.                     flg_zone=FALSE;
  342.                 }
  343.             }  while(flg_zone==TRUE);
  344.             max_border=num_bord;
  345.             Flg_Err=FALSE;
  346.         }
  347.     }
  348.     return Flg_Err;
  349. }
  350.  
  351.  
  352. /*********************************************************** read_zones() ***/
  353.  
  354. BOOL    
  355. read_zones(char *p_filename, struct  RastPort        *rpG)
  356.  
  357. /* lecture du fichier .zones */
  358. /// "read_zones"
  359. {
  360.     FILE *input;
  361.     int mx,my,MX,MY;
  362. /*      struct RastPort *r;*/
  363.     BOOL    Flg_Err;
  364.  
  365.     char str[100];
  366.     char    *P_Temp;
  367.     char    Buffer_Name[100];
  368.     int     flg_country;
  369.     int     flg_end;
  370.     int     num_country;
  371.     int     num_bord;
  372.     int     tab_bord_tempo[MAX_VECT];
  373.     int     i,*p_alloc;
  374.     /* Vecteur courant */
  375.     VECT    *V;
  376.     int     num_vect,code;
  377.     BOOL    Flg_Draw;
  378.     
  379.     Flg_Err=TRUE;
  380.  
  381.     num_country=0;
  382.     flg_end=FALSE;
  383. #ifdef DRAW
  384.     r = rpG;
  385.     SetRast(r,COLOR_BLEU+num_first_color);
  386.     SetAPen(r,COLOR_COTE+num_first_color);
  387.     SetBPen(r,COLOR_COTE+num_first_color);
  388. #endif
  389.  
  390.     sprintf(Buffer_Name,"%s.zones",p_filename);
  391.  
  392.  
  393.     if (input = fopen(Buffer_Name,"r"))
  394.     {
  395.  
  396.         do
  397.         {
  398.             /***************************************************/
  399.             /* D'abord le nom de la zone                       */
  400.             /* On alloue de la memoire pour sauvegarder ce nom */
  401.             /* et on le recopie                                */
  402.             /***************************************************/
  403.             Dtb_Init_Country(&tab_country[num_country]);
  404.             fscanf(input,"%d",&tab_country[num_country].continent);
  405.  
  406.             /* Lecture du reste de la ligne */
  407.             P_Temp=fgets(str,100,input);
  408.             if(P_Temp!=NULL)
  409.             {
  410.                 /* Enleve les blancs */
  411.                 P_Temp=stpblk(P_Temp);
  412. #ifdef  TOMDEBUG
  413.                 printf("Country name:%s\n",P_Temp);
  414. #endif
  415.                 /* Enleve le newline de fin de ligne */
  416.                 P_Temp[strlen(P_Temp)-1]=0;
  417.                 tab_country[num_country].name=strdup(P_Temp);
  418.             }
  419.  
  420.             /* Debut du dessin */
  421.  
  422.             flg_country=TRUE;
  423.  
  424.             /************************************************/
  425.             /* On recupere les numeros de border de ce pays */
  426.             /* puis pour chaque bord on affiche ses vecteurs*/
  427.             /************************************************/
  428.             mx=100000;
  429.             my=100000;
  430.             MX=-100000;
  431.             MY=-100000;
  432.             num_bord=0;
  433.             code=0;
  434.             do
  435.             {
  436.                 fscanf(input,"%d",&num_vect);
  437. #ifdef TOMDEBUG
  438.                 printf("Vecteur num:%d\n",num_vect);
  439. #endif
  440.                 tab_bord_tempo[num_bord]=num_vect;
  441.  
  442.  
  443.                 V=tab_vect[num_vect].p_vect;
  444.  
  445.                 Flg_Draw=TRUE;
  446.  
  447.                 if(V[0].Code>1000)
  448.                 {
  449.                     code=V[0].Code/1000;
  450.                     if((code==ID_LAC)||(code==ID_RIVIERES))
  451.                     {
  452.                         Flg_Draw=FALSE;
  453.                     }
  454.                 }
  455.                 if(Flg_Draw)
  456.                 {
  457.                     for(i=0;i<tab_vect[num_vect].nb_vect;i++)
  458.                     {
  459. #ifdef DRAW
  460.                         if(i==0)
  461.                         {
  462.                                 MyMove(r,V[0].x,V[0].y);
  463.                         }
  464.                         else
  465.                         {
  466.                                 MyDraw(r,V[i].x,V[i].y,V[i].Code);
  467.                         }
  468. #endif
  469.                         tstpt(V[i].x,V[i].y,&mx,&my,&MX,&MY);
  470.                     }
  471.                 }
  472.                 num_bord++;
  473.                 if(num_bord>=MAX_VECT)
  474.                 {
  475.                     printf("ERROR:Max number of border exceeded\n");
  476.                     flg_end=TRUE;
  477.                 }
  478.  
  479.             }  while(tst_end(input)==FALSE);
  480.             if (MX<=mx)
  481.             {
  482.                 mx=V[0].x-5;
  483.                 MX=V[0].x+5;
  484.             }
  485.             if (MY<=my)
  486.             {
  487.                 my=V[0].y-5;
  488.                 MY=V[0].y+5;
  489.             }                               
  490.             tab_country[num_country].c_clip.minx=mx;
  491.             tab_country[num_country].c_clip.miny=my;
  492.             tab_country[num_country].c_clip.maxx=MX;
  493.             tab_country[num_country].c_clip.maxy=MY;
  494. #ifdef DRAW
  495.             MyMove(r,mx,my);
  496.             MyDraw(r,MX,my,5);
  497.             MyDraw(r,MX,MY,5);
  498.             MyDraw(r,mx,MY,5);
  499.             MyDraw(r,mx,my,5);
  500.  
  501. #endif
  502.             /****************************************/
  503.             /* On sait le nombre de bord de ce pays */
  504.             /* on leur alloue de la memoire et on le*/
  505.             /* recopie                              */
  506.             /****************************************/
  507.  
  508. #ifdef DEBUG_TOM
  509.             printf("Il y a eu:%d bord\n",num_bord);
  510. #endif
  511.             tab_country[num_country].nb_borders=num_bord;
  512.             p_alloc=(int *)malloc((10+num_bord)*sizeof(int));
  513.             tab_country[num_country].p_borders=p_alloc;
  514.             for(i=0;i<num_bord;i++)
  515.             {
  516.                 *p_alloc=tab_bord_tempo[i];
  517. /*                              printf("%d %d\n",i,tab_bord_tempo[i]);*/
  518.                 p_alloc++;
  519.             }
  520.             num_country++;
  521.             if (num_country>=MAX_COUNTRY)
  522.             {
  523.                 printf("ERROR:Max number of country excedeed\n");
  524.                 flg_end=TRUE;
  525.             }
  526.         }  while((tst_end(input)==FALSE)&&(flg_end==FALSE));
  527.  
  528.         fclose(input);
  529.         Flg_Err=FALSE;
  530.     }
  531.     max_country=num_country;
  532.     return(Flg_Err);
  533. }
  534. ///
  535.  
  536.  
  537. /*********************************************************** save_zones() ***/
  538.  
  539. void    
  540. save_zones( char *p_filename)
  541. {
  542.     FILE *output;
  543.     int     num_country;
  544.     int     i;
  545.  
  546.  
  547.  
  548.  
  549.     if (output = fopen(p_filename,"w"))
  550.     {
  551.             for(num_country=0;num_country<max_country;num_country++)
  552.             {
  553.                     fprintf(output,"%d ",tab_country[num_country].continent);
  554.                     fprintf(output,"%s\n",tab_country[num_country].name);
  555.  
  556.  
  557.  
  558.  
  559.                     for(i=0;i<tab_country[num_country].nb_borders;i++)
  560.                     {
  561.  
  562.                     
  563.                             fprintf(output,"%d ",tab_country[num_country].p_borders[i]);
  564.  
  565.                     }
  566.                     fprintf(output,"E\n");
  567.  
  568.             }
  569.             fclose(output);
  570.     }
  571. }
  572.  
  573. void close_files(void)
  574. {
  575.     if (bptr_big!=NULL)
  576.         Close(bptr_big);
  577. }
  578.